package com.yss.reportworld.service.impl;

import com.yss.common.core.constant.SecurityConstants;
import com.yss.common.core.domain.YssResponse;
import com.yss.common.core.enums.YssConfigEnum;
import com.yss.common.core.utils.DateUtils;
import com.yss.common.core.utils.HolidayUtils;
import com.yss.common.core.utils.StringUtils;
import com.yss.reportworld.service.IWeComService;
import com.yss.system.api.RemoteConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 企业微信机器人发送短信
 */
@Service
@Slf4j
public class WeComServiceImpl implements IWeComService {
    @Autowired
    private RemoteConfigService remoteConfigService;

    /**
     * 企业微信机器人发送短信
     */
    public void sendMessage(String url, String message) {
        try {
            if (StringUtils.isBlank(url) || StringUtils.isBlank(message)) {
                return;
            }
         /*   url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=23e0f483-41fc-4f3e-bdcf-e66314ccaa10";
            message = "测试机器人";*/
            HttpHeaders header = new HttpHeaders();
            header.setContentType(MediaType.APPLICATION_JSON);
            header.set("Authorization", "APPCODE_PD");

            Map<String, Object> requestParam = new HashMap<>();
            Map<String, Object> content = new HashMap<>();
            content.put("content", message);

            requestParam.put("msgtype", "text");
            requestParam.put("text", content);
            HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(requestParam, header);

            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
            restTemplate.postForEntity(url, request, String.class);
        } catch (Exception ex) {
            log.error("发送消息失败。" + ex.getMessage());
        }
    }

    /**
     * 企业微信机器人发送短信 地址和@的用户是配置的脚本审核人员
     */
    public void sendMessageTo(String message) {
        YssResponse<String> responsename = remoteConfigService.getConfigByKey(YssConfigEnum.REPORTWORLD_WX_SEND_USERNAME.getCode(), SecurityConstants.INNER);
        String username = responsename.getData();
        YssResponse<String> responseurl = remoteConfigService.getConfigByKey(YssConfigEnum.REPORTWORLD_WX_SEND_WECOMURL.getCode(), SecurityConstants.INNER);
        String url = responseurl.getData();
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(url)) {
            log.info("企业微信消息发送失败没有配置用户名和地址！");
            return;
        }
        this.sendMessage(url, username + " " + message);
    }

    /**
     * 企业微信机器人发送短信
     */
    public void sendMessage(String message) {
        YssResponse<String> responseurl = remoteConfigService.getConfigByKey(YssConfigEnum.REPORTWORLD_WX_SEND_WECOMURL.getCode(), SecurityConstants.INNER);
        String url = responseurl.getData();
        this.sendMessage(url, message);
    }

    /**
     * 企业微信机器人发送短信 地址和@的用户是配置的消息接受人
     */
    public void sendMessageToManage(String message) {
        try {
            String yearHoliday = HolidayUtils.isWorkingDay(DateUtils.dateToStrByFormat(new Date(), DateUtils.YYYY_MM_DD));
            if (!"0".equals(yearHoliday)) {
                log.info("节假日不发送消息");
                return;
            }
            YssResponse<String> responsename = remoteConfigService.getConfigByKey(YssConfigEnum.REPORTWORLD_WX_SEND_USERNAME_MANAGE.getCode(), SecurityConstants.INNER);
            String username = responsename.getData();
            YssResponse<String> responseurl = remoteConfigService.getConfigByKey(YssConfigEnum.REPORTWORLD_WX_SEND_WECOMURL_MANAGE.getCode(), SecurityConstants.INNER);
            String url = responseurl.getData();
            if (StringUtils.isEmpty(username) || StringUtils.isEmpty(url)) {
                log.info("企业微信消息发送失败没有配置用户名和地址！");
                return;
            }
            this.sendMessage(url, username + " " + message);
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
    }
}
